package com.fan.admin.controller;

import com.fan.admin.service.CourseRelationService;
import com.fan.admin.service.CourseService;
import com.fan.admin.service.StudentScoresService;
import com.fan.admin.service.TeacherService;
import com.fan.admin.util.LocalCache;
import com.fan.commons.pojo.Course;
import com.fan.commons.pojo.StudentScores;
import com.fan.commons.pojo.Teacher;
import com.fan.commons.util.JsonResult;
import com.fan.commons.util.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author fan
 * @date 2021/1/29
 */
@RestController
public class TeacherController {

	@Autowired
	private TeacherService teacherService;

	@Autowired
	private CourseService courseService;

	@Autowired
	private CourseRelationService courseRelationService;

	@Autowired
	private StudentScoresService studentScoresService;

	@Autowired
	private LocalCache localCache;


	@PostMapping("/teacher/add")
	public JsonResult addTeacher(Teacher teacher) {
		// teacher 在添加之前要完善 teacher 中的信息，并且后端要完成验证
		String serviceTeacherId = teacherService.getTeacherId(teacher.getCollegeId(), teacher.getSystemId());

		int count = Integer.parseInt(serviceTeacherId.substring(8)) + 1;
		teacher.setPassword(teacher.getEmail());
		teacher.setRole(2);
		// 生成新的教师号
		String teacherId = teacher.getCollegeId() + teacher.getSystemId() + count;
		teacher.setTeacherId(teacherId);

		int status = teacherService.insertTeacher(teacher);
		if (status == 1) {
			return JsonResult.success(null);
		}
		return JsonResult.failure();
	}

	@GetMapping("/teacher/list")
	public JsonResult teacherList(Teacher teacher, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer pageSize) {
		// 分页查询
		PageHelper.startPage(page, pageSize);
		List<Teacher> teachers = teacherService.queryTeachers(teacher);
		if (teachers.size() == 0) {
			return JsonResult.failure();
		}
		for (Teacher tea : teachers) {
			tea.setCollegeId(localCache.getCollegeInfoCache(tea.getCollegeId()).getName());
			tea.setSystemId(localCache.getSystemInfoCache(tea.getSystemId()).getName());
		}
		PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);
		PageUtil pageUtil = new PageUtil(pageInfo.getPageNum(), pageInfo.getPageSize(), teachers, pageInfo.getTotal());
		return JsonResult.success(pageUtil);
	}


	@GetMapping("/teacher/query/{id}")
	public JsonResult teacherQueryById(@PathVariable Integer id) {
		List<Teacher> teachers = teacherService.queryTeachers(new Teacher().setId(id));
		if (teachers.size() == 1) {
			return JsonResult.success(teachers.get(0));
		}
		return JsonResult.failure();
	}


	@PutMapping("/teacher/update")
	public JsonResult teacherUpdate(Teacher teacher) {
		System.out.println(teacher);
		String teacherId = teacher.getTeacherId();
		if (!teacherId.contains(teacher.getCollegeId()) || !teacherId.contains(teacher.getSystemId())) {
			// 院系修改，修改对应的 teacher_id
			String serviceTeacherId = teacherService.getTeacherId(teacher.getCollegeId(), teacher.getSystemId());
			int count = Integer.parseInt(serviceTeacherId.substring(8)) + 1;
			teacherId = teacher.getCollegeId() + teacher.getSystemId() + count;
			teacher.setTeacherId(teacherId);
		}
		int code = teacherService.updateTeacherById(teacher);
		if (code == 1) {
			return JsonResult.success(null);
		}
		return JsonResult.failure();
	}

	@GetMapping("/teacher/delete")
	public JsonResult teacherDeleteById(@RequestParam Integer id) {
		int code = teacherService.deleteTeacherById(id);
		if (code == 1) {
			return JsonResult.success(null);
		}
		return JsonResult.failure();
	}

	@GetMapping("/teacher/course/must/")
	public JsonResult teacherCourseCla(HttpServletRequest request) {
		List<Course> courses = teacherService.teacherCourseCla("c002s0041000");
		PageUtil pageUtil = new PageUtil(1, courses.size(), courses, (long) courses.size());
		return JsonResult.success(pageUtil);
	}

	@GetMapping("/teacher/course/choose/")
	public JsonResult teacherCourseStudent(HttpServletRequest request) {
		List<Course> courses = courseService.queryCourseActive(new Course().setTeacherId("c002s0041000").setNature(1));
		PageUtil pageUtil = new PageUtil(1, courses.size(), courses, (long) courses.size());
		return JsonResult.success(pageUtil);
	}

	@GetMapping("/teacher/{courseId}")
	public JsonResult teacherCourse(@PathVariable String courseId) {
		Course course = courseService.queryCourseByCourseId(courseId);
		return JsonResult.success(course);
	}

	@PutMapping("/teacher/course/update/{courseId}")
	public JsonResult teacherUpdateCourse(@PathVariable String courseId, Course course) {
		course.setCourseId(courseId);
		course.setTeacherId("c002s0041000");
		int coed = courseService.updateCourseScore(course);
		if (coed != 1) {
			return JsonResult.failure();
		}
		return JsonResult.success(null);
	}

	@GetMapping("/teacher/course/students/{courseId}")
	public JsonResult teacherStudentsScores(@PathVariable String courseId) {
		List<StudentScores> studentScores = studentScoresService.selectByCourseId(courseId);
		PageUtil pageUtil = new PageUtil(1, studentScores.size(), studentScores, (long) studentScores.size());
		return JsonResult.success(pageUtil);
	}

	@GetMapping("/teacher/student/score/{id}")
	public JsonResult teacherStudentScore(@PathVariable Integer id) {
		StudentScores studentScores = studentScoresService.selectByPrimaryKey(id);
		return JsonResult.success(studentScores);
	}

	@PutMapping("/teacher/student/course/update/")
	public JsonResult teacherUpdateStudentScore(StudentScores studentScores) {

		int code = studentScoresService.updateByPrimaryKeySelective(studentScores);
		if (code != 1) {
			return JsonResult.failure();
		}
		return JsonResult.success(null);
	}
}
